
export const plantForm = process.env.UNI_PLATFORM;
console.log(plantForm);

// 支付宝小程序环境
export const isAlipay = plantForm == 'mp-alipay';

// 微信小程序环境
export const isWechat = plantForm == 'mp-weixin';

// 百度小程序环境
export const isBaidu = plantForm == 'mp-baidu';

// 小程序环境
export const isMini = isAlipay || isWechat || isBaidu;

// h5环境
export const isH5 = plantForm == 'h5';

// 浙里办APP
export const isZlb = isH5 && navigator && navigator.userAgent.indexOf('@ZLB') > -1;

// 浙里办支付宝
export const isZlbAlipay = isH5 && navigator && navigator.userAgent.indexOf('AliApp') > -1;

// 浙里办微信
export const isZlbWechat = isH5 && navigator && navigator.userAgent.indexOf('miniProgram') > -1;

// 浙里办所有环境
export const isZlbEnv = isZlb || isZlbAlipay || isZlbWechat;

// 引入对应jssdk
export function setJssdk() {
  const color = 'color: red;font-size: 20px';
  if (isAlipay) {
    console.log('%c 当前环境：支付宝小程序', color);
  } else if (isWechat) {
    console.log('%c 当前环境：微信小程序', color);
  } else if (isBaidu) {
    console.log('%c 当前环境：百度小程序', color);
  } else if (isZlb) {
    loadZlbJs();
    console.log('%c 当前环境：浙里办APP', color);
  } else if (isZlbAlipay) {
    loadZlbJs();
    console.log('%c 当前环境：浙里办支付宝', color);
  } else if (isZlbWechat) {
    loadZlbJs();
    console.log('%c 当前环境：浙里办微信', color);
  } else if (isH5) {
    console.log(navigator.userAgent);
    console.log(location.href);
    console.log('%c 当前环境：h5', color);
  }
}

export function appendJS(url) {
  var head = document.head || document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.setAttribute('src', url);
  head.appendChild(script);
}

export function loadZlbJs() {
  document.writeln('<script type="text/javascript" src="//assets.zjzwfw.gov.cn/assets/ZWJSBridge/1.1.0/zwjsbridge.js"></script>');
  setTimeout(() => {
    ZWJSBridge.onReady(() => {
      console.log('ready');
    });
  }, 300);
}

// function loadScript(url, callback) {
//   const head = document.getElementsByTagName('head')[0];
//   const script = document.createElement('script');
//   script.type = 'text/javascript';
//   script.src = url;
//   script.onload = script.onreadystatechange = function() {
//     if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
//       callback && callback();
//       script.onload = script.onreadystatechange = null;
//       if (head && script.parentNode) {
//         head.removeChild(script);
//       }
//     }
//   };
//   head.insertBefore(script, head.firstChild);
// }
